<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

use herosphp\core\Loader;


class FriendLinkAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();
	}
	protected $serviceBean="admin.settingFriendlink.service";

	public function add(HttpRequest $request) {
	
		parent::add($request);
		$catidoptions = Loader::model('settingFriendlinkCate')->getItems();
		$this->assign('catidoptions',$catidoptions);
		$this->assign("admin_name",adminName());
		$this->assign("title", "添加友情链接");
		$this->assign('bread', array('网站设置','友情链接',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/friendLink/index'),'javascript:;'));
	
	}

	public function edit(HttpRequest $request) {
		$catidoptions = Loader::model('settingFriendlinkCate')->getItems();
		$this->assign('catidoptions',$catidoptions);
		$this->assign("admin_name",adminName());
		parent::edit($request);
		$this->assign("title", "编辑友情链接");
		$this->assign('bread', array('网站设置','友情链接',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/friendLink/index'),'javascript:;'));
	}

	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','友情链接');
		$s_title = $request->getParameter('s_title');
		$this->assign('s_title',$s_title);
		if($s_title !== '' && $s_title!== NULL)$conditions['title'] = '%'.$s_title.'%';
		$s_catid = $request->getParameter('s_catid');
		$this->assign('s_catid',$s_catid);
		if($s_catid !== '' && $s_catid!== NULL)$conditions['catid'] = $s_catid;
		$s_addtime = $request->getParameter('s_addtime');
		$this->assign('s_addtime',$s_addtime);
		if($s_addtime !== '' && $s_addtime!== NULL)$conditions['addtime'] = '%'.$s_addtime.'%';
		$s_url = $request->getParameter('s_url');
		$this->assign('s_url',$s_url);
		if($s_url !== '' && $s_url!== NULL)$conditions['url'] = '%'.$s_url.'%';
		$s_logo = $request->getParameter('s_logo');
		$this->assign('s_logo',$s_logo);
		if($s_logo !== '' && $s_logo!== NULL)$conditions['logo'] = '%'.$s_logo.'%';
		$s_type = $request->getParameter('s_type');
		$this->assign('s_type',$s_type);
		if($s_type !== '' && $s_type!== NULL)$conditions['type'] = '%'.$s_type.'%';
		$s_listorder = $request->getParameter('s_listorder');
		$this->assign('s_listorder',$s_listorder);
		if($s_listorder !== '' && $s_listorder!== NULL)$conditions['listorder'] = $s_listorder;
		$s_status = $request->getParameter('s_status');
		$this->assign('s_status',$s_status);
		if($s_status !== '' && $s_status!== NULL)$conditions['status'] = '%'.$s_status.'%';
		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->assign('_filter',array('title','setting_friendlink_cate_id','addtime','url','logo','type_text','listorder','app_id'));
		$this->assign('_header',array('标题','分类','添加时间','网址','LOGO','类型','排序','appid'));
		$this->setConditions($conditions);
	
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);

		$this->page = $request->getParameter('page', 'intval');
		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		$service = Beans::get($this->getServiceBean());
		$total = $service->count($this->getConditions());
		$items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
				array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
		//初始化分页类
		$this->generatePageMenu($total);
		$this->assign('total',$total);

		//parent::index($request);
		$catidoptions = Loader::model('settingFriendlinkCate')->getItems();
		$this->assign('catidoptions',$catidoptions);
		$this->assign("title", "友情链接");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->assign('items',$items);
		$this->setView(strtolower($request->getAction()) . '/index');
	}
	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		if(!$data['status']){$data['status'] = 0;}
	
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		if(!$data['status']){$data['status'] = 0;}
		$request->addParameter('data',$data);
		parent::do_edit($request);	
	}
	
	

}
